package com.springinaction.ch05.data;

import com.springinaction.ch05.Spitter;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * @author ssk
 * @Title: SpitterRepositorylmpl
 * @ProjectName SpringInAction
 * @Description: TODO
 * @date 18-12-9下午6:27
 */
@Component
public class SpitterRepositorylmpl implements SpitterRepository {
    private  List<Spitter> spitterList = createSpitterList();
    @Override
    public Spitter save(Spitter spitter) {
        spitter.setId(Long.valueOf(spitterList.size()));
        spitterList.add(spitter);
        System.out.print(spitter);
        return spitter;
    }

    @Override
    public Spitter findByUsername(String username) {
        for (int i = 0; i < spitterList.size(); i++) {
            if (spitterList.get(i).getUsername().equals(username)) {
                return spitterList.get(i);
            }
        }
        return spitterList.get(0);
    }
    private List<Spitter> createSpitterList() {
        List<Spitter> spitterTempList = new LinkedList<Spitter>();
        for (int i = 0; i < 24; i++) {
            spitterTempList.add(new Spitter(Long.valueOf(i), Integer.toString(i + 1), Integer.toString(i + 2),
                    Integer.toString(i + 3), Integer.toString(i + 4)));
        }
        return spitterTempList;
    }
}
